package com.zjs.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjs.bean.User;
import com.zjs.config.Result;
import com.zjs.mapper.UserMapper;
import com.zjs.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserMapper userMapper;

    @PostMapping
    public Result<?> save(@RequestBody User user) {
        userMapper.insert(user);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody User user) {
        userMapper.updateById(user);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        userMapper.deleteById(id);
        return Result.success();
    }

    //@GetMapping
    //public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
    //                          @RequestParam(defaultValue = "5") Integer pageSize,
    //                          @RequestParam(defaultValue = "") String search) {
    //    LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery().orderByAsc(User::getNickName, );
    //    if (!StrUtil.isBlank(search)) {
    //        wrapper.like(User::getNickName, search);
    //    }
    //    Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize),wrapper);
    //    return Result.success(userPage);
    //}

    @GetMapping

    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam (defaultValue = "")String search){

        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(User::getNickName, search);
        }
        Page<User> userPage = userMapper.findPage(new Page<>(pageNum, pageSize),search);

        //Page<User> userPage=userMapper.selectPage(new Page<>(pageNum,pageSize),Wrappers.<User>lambdaQuery().like(User::getNickName,search));
        return Result.success(userPage);
    }


    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(userMapper.selectList(null));
    }
    @GetMapping("/{id}")
    public Result<?> userById(@PathVariable Integer id) {
        return Result.success(userMapper.selectById(id));
    }




    /**
     * 登入
     * @param userParam
     * @return
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User userParam) {
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, userParam.getUsername())
                .eq(User::getPassword, userParam.getPassword())
                .eq(User::getRole, userParam.getRole())

        );
        if(user == null) {
            return Result.error("-1", "用户名或者密码错误");
        }
        // 生成token
        String token = TokenUtils.genToken(user);
        user.setToken(token);
        //userMapper.insert(user);
        return Result.success(user);

    }


    @PostMapping("/register")
    public Result<?> register(@RequestBody User userParam) {
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUsername, userParam.getUsername())

        );
        if(user != null) {
            return Result.error("-1", "用户名重复");
        }
       if(userParam.getPassword() == null) {
            user.setPassword("123456");
        }
       userMapper.insert(userParam);
        //userMapper.insert(user);
        return Result.success();

    }



    /**
     * 统计数据
     * @return
     */
    @GetMapping("/count")
    public Result<?> count() {
        //当前登录的用户信息
        User user = getUser();
        return Result.success(userMapper.countAddress());
    }




}
